<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class UtilisateurManager{
    
    private $bdd;
    
    function __construct($bdd) {
        $this->bdd = $bdd;
    }
    
    public function createUserFb($user){
        
            $req = $this->bdd->prepare('SELECT idUser FROM UTILISATEURS WHERE idFace = ?');
            $req->execute(array($user->getId()));
            $donnees = $req->fetchAll();$id=0;
            
            if(empty($donnees)){
                 $req2 = $this->bdd->prepare('INSERT INTO UTILISATEURS (Nom, Prenom,idFace) VALUES(:nom, :prenom ,:idface)');
                 $req2->execute(array('nom' => $user->getLastName(),'prenom' => $user->getFirstName(),'idface' => $user->getId()));
            }else{
                 $id =  $donnees[0][0];
            }
            return $id;
    }
    
    public function createUserGoogle($user){
        
            $req = $this->bdd->prepare('SELECT idUser FROM UTILISATEURS WHERE idGoogle = ?');
            $req->execute(array((string)$user->getId()));
            $donnees = $req->fetchAll();$id=0;
            
            if(empty($donnees)){
                 $req2 = $this->bdd->prepare('INSERT INTO UTILISATEURS (Nom, Prenom,idGoogle,cheminPhoto) VALUES(:nom, :prenom ,:idgoogle,:link)');
                 $req2->execute(array('nom' => $user->getFamilyName(),'prenom' => $user->getGivenName(),'idgoogle' => (string)$user->getId(),'link' => $user->getPicture()));
            }else{
                 $id =  $donnees[0]['idUser'];
            }
            
            return $id;
    }
    
    
}

